package org.jetlinks.community.configuration;


import org.jetlinks.community.constant.RabbitmqConstant;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfigurator {

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }



    //声明物联网到业务平台交换机
    @Bean(RabbitmqConstant.IOT2BIZ_EXCHANGE)
    public Exchange sendDirectExchange(){
        return ExchangeBuilder.directExchange(RabbitmqConstant.IOT2BIZ_EXCHANGE).durable(true).build();
    }

    //声明物联网到业务平台队列
    @Bean(RabbitmqConstant.IOT2BIZ_QUEUE)
    public Queue sendTopicQueue(){
        return QueueBuilder.durable(RabbitmqConstant.IOT2BIZ_QUEUE).build();
    }

    //绑定队列和交换机
    @Bean
    public Binding sendExchangeQueueBind(@Qualifier(RabbitmqConstant.IOT2BIZ_QUEUE) Queue queue,
                                     @Qualifier(RabbitmqConstant.IOT2BIZ_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(RabbitmqConstant.IOT2BIZ_BIND_KEY).noargs();
    }


    //声明业务平台到物联网队列
    @Bean(RabbitmqConstant.BIZ2IOT_QUEUE)
    public Queue receiveTopicQueue(){
        return QueueBuilder.durable(RabbitmqConstant.BIZ2IOT_QUEUE).build();
    }


    //声明物联网到业务平台队列
    @Bean(RabbitmqConstant.IOT2BIZ_QUEUE1)
    public Queue sendTopicQueue1(){
        return QueueBuilder.durable(RabbitmqConstant.IOT2BIZ_QUEUE1).build();
    }

    //绑定队列和交换机
    @Bean
    public Binding sendExchangeQueueBind1(@Qualifier(RabbitmqConstant.IOT2BIZ_QUEUE1) Queue queue,
                                         @Qualifier(RabbitmqConstant.IOT2BIZ_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(RabbitmqConstant.IOT2BIZ_BIND_KEY).noargs();
    }
}
